package com.mls.fivedistrict.controller.portal;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.pojo.Vote;
import com.mls.fivedistrict.server.VoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by chenxiuxiang on 2018/6/7.
 */
@RestController("votePortalController")
@RequestMapping("/api/app")
public class VoteController extends BaseController {
    @Autowired
    private VoteService voteService;

    @GetMapping("/v1/votes")
    public ServiceResponse getVoteList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List list = voteService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo(list, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    @GetMapping("/v1/vote/{voteId}")
    public ServiceResponse getVoteDetail(@PathVariable Long voteId) {
        Vote vote = voteService.selectById(voteId);
        if (vote == null) {
            return ServiceResponse.createErrorResponse("投票不存在");
        }
        return ServiceResponse.createSuccessResponse(vote);
    }
}
